package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public void save(User user) {
        if (userMapper.selectByEmailAndPassword(user.getEmail(), null)!=null) {
            throw new RuntimeException("该邮箱已被注册");
        }
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("/img/user/bg.jpeg");
        userMapper.insert(user);
    }

    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if (user == null){
            throw new RuntimeException("邮箱和密码不匹配");
        }
        return user;
    }

    public PageInfo<?> list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectAll(qo);
        return new PageInfo<>(list);
    }

    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    public User getAuthorById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }


    public void updateBalance(User user) {
        userMapper.updateBalance(user);
    }

    public User getById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public User getByEmail(String email) {
        return userMapper.selectByEmailAndPassword(email,null);
    }

    @Override
    public User getUserByUserId(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public List<User> getCares(Long userId) {
        return userMapper.getCaresById(userId);
    }

    public List<User> getFans(Long userId) {
        return userMapper.getFansById(userId);
    }
}
